﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using InterfaceXAML.ExaPlanningService;
using InterfaceXAML.ModelBinding;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Text;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// Pour en savoir plus sur le modèle d'élément Contrôle utilisateur, consultez la page http://go.microsoft.com/fwlink/?LinkId=234236

namespace InterfaceXAML.UserControls
{
    public sealed partial class AutoComplete : UserControl
    {
        private List<LeaderProjectSimpleModel> _leaderProject = new List<LeaderProjectSimpleModel>();

        public List<LeaderProjectSimpleModel> LeaderProjectSet { get { return _leaderProject; } set { _leaderProject = value; } }

        private LeaderProjectModel _leaderProjectModel = new LeaderProjectModel();

        public LeaderProjectModel LeaderProjectModel { get { return _leaderProjectModel; } set { _leaderProjectModel = value; } }

        public AutoComplete()
        {
            this.InitializeComponent();
        }

        private void DisplayPopup(object sender, RoutedEventArgs e)
        {
            Popup.IsOpen = !Popup.IsOpen;
        }

        private async void LeaderProjectText_TextChanged(object sender, RoutedEventArgs e)
        {
            var leaderProject = "";
            LeaderProjectText.Document.GetText(TextGetOptions.NoHidden, out leaderProject);

            if (leaderProject.Length < 3)
            {
                Popup.IsOpen = false;
                return;
            }
            Popup.IsOpen = true;

            // Appel au WS et mise à jour de LeaderProject
            AutoComplete_ListBox.ItemsSource = await App.ExaPlanningServiceClient.GetLeaderProjectAsync(leaderProject);
        }

        private void AutoComplete_ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count == 0)
            {
                LeaderProjectText.TextChanged -= LeaderProjectText_TextChanged;
                LeaderProjectText.Document.SetText(TextSetOptions.Unhide, String.Empty);
                LeaderProjectText.TextChanged += LeaderProjectText_TextChanged;
                return;
            }
            
            var selection = (e.AddedItems.ElementAt(0)) as LeaderProjectSimpleModel;
            LeaderProjectText.TextChanged -= LeaderProjectText_TextChanged;
            LeaderProjectText.Document.SetText(TextSetOptions.Unhide, selection.Name + " " + selection.FirstName);
            LeaderProjectText.TextChanged += LeaderProjectText_TextChanged;
        }


        public void DisplayError()
        {
            WrapperError.BorderBrush = Application.Current.Resources["ErrorField"] as SolidColorBrush;
            return;
        }

        public void DisplayNormal()
        {
            WrapperError.BorderBrush = Application.Current.Resources["CorrectField"] as SolidColorBrush;
            return;
        }

    }
}
